package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.RedisConst;
import com.itheima.exception.BusinessRuntimeException;
import com.itheima.pojo.Setmeal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.Map;

/**
 * @ClassName SetmealServiceImpl
 * @Description: TODO
 * @Author 郑乾
 * @Date 2020-06-25
 * @Version V1.0
 **/
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealDao setmealDao;
    @Autowired
    JedisPool jedisPool;

    @Override
    @Transactional
    public void add(Setmeal setmeal, Integer[] checkgroupIds) {
        setmealDao.add(setmeal);
        //维护套餐和检查组关系
        if (setmeal.getId() != null && checkgroupIds != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.setSetmealWithCheckgroup(setmeal.getId(), checkgroupId);
            }
        }
        //添加套餐完成 把图片从redis中移除,否则会作为辣鸡图片删掉
        jedisPool.getResource().srem(RedisConst.SETMEAL_PIC_RESOURCES, setmeal.getImg());
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Setmeal> pageSetmeal = setmealDao.findByCondition(queryPageBean.getQueryString());
        return new PageResult(pageSetmeal.getTotal(), pageSetmeal.getResult());
    }

    @Override
    public void delById(Integer id) {
        //查询套餐是否被关联
        long count = setmealDao.findCountById(id);
        if (count > 0) {
            throw new BusinessRuntimeException("该套餐正在被使用,无法删除");
        }
        //删除和检查组的关联关系
        setmealDao.delRelation(id);
        //删除套餐
        setmealDao.delById(id);
    }

    @Override
    public Setmeal findById(Integer id) {
        return setmealDao.findById(id);
    }

    @Override
    public List<Integer> findCheckgroupIdsWithSetmeal(Integer id) {
        return setmealDao.findCheckgroupIdsWithSetmeal(id);
    }

    @Override
    @Transactional
    public void edit(Setmeal setmeal, Integer[] checkgroupIds) {
        //删除所有检查组关联
        setmealDao.delRelation(setmeal.getId());
        //维护新的关系
        setSetmealWithCheckgroup(setmeal, checkgroupIds);
        //编辑套餐
        setmealDao.edit(setmeal);
    }

    @Override
    public List<Setmeal> findAll() {
        return setmealDao.findAll();
    }

    @Override
    public Setmeal findDetailsById(Integer id) {
        return setmealDao.findDetailsById(id);
    }

    @Override
    public List<Map> getSetmealReport() {
        return setmealDao.getSetmealReport();
    }

    private void setSetmealWithCheckgroup(Setmeal setmeal, Integer[] checkgroupIds) {
        if (setmeal.getId() != null && checkgroupIds.length > 0) {
            for (Integer checkgroupId : checkgroupIds) {
                setmealDao.setSetmealWithCheckgroup(setmeal.getId(), checkgroupId);
            }
        }
    }
}
